//main engine class
function Engine(){
	this.containers = null;
	this.uniqeIdent = null;
	this.view = null;
	this.repo = null;
	
	this.init = function(params){
		this.containers = params.containers;
		this.uniqeIdent = params.uniqeIdent;
	};
	
	this.getHeadId = function(){
		return this.containers.head;
	};
	
	this.getBodyId = function(){
		return this.containers.body;
	};
	
	this.getParentId = function(){
		return this.containers.parent;
	};
	
	this.getUniqueIdent = function(){
		return this.uniqeIdent;
	};
	
}

var EngineModel = function(){
    this.attribs = {};
    this.setIdent = function(ident){
        if(typeof ident == 'undefined'){
            this.attribs.ident = utils.getUniqueId();
        }else{
            this.attribs.ident = ident;
        }
    };
    this.setDateCreatedTimeStamp = function(){
        this.attribs.dateCreatedTimeStamp = utils.getCurrTimeStamp();  
    };
    
    this.setDateCreated = function(dateCreated){
        if(typeof dateCreated == 'undefined'){
            this.attribs.dateCreated = utils.getCurrDate();  
        }else{
            this.attribs.dateCreated = dateCreated;  
        }
        
    };
    
    this.getIdent = function(){
        return this.attribs.ident;
    };
    
    this.getDateCreatedTimeStamp = function(){
        return this.attribs.dateCreatedTimeStamp;
    };
    
    this.getDateCreated = function(){
        return this.attribs.dateCreated;
    };
    
    this.setDbIdent = function(dbIdent){
        this.attribs.dbIdent = dbIdent;
    };
    
    this.getDbIdent = function(){
        return this.attribs.dbIdent;
    };
};

var utils = {
    //unique id generation
    getUniqueId: function(){
        function getRandomNumber(range){
            return Math.floor(Math.random() * range);
        }
        
        function getRandomChar(){
            var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
            //var charSet2 = utils.getCurrTimeStamp();
            var char1 = chars.substr( getRandomNumber(62), 1 );
            //var char2 = charSet2.substr( getRandomNumber(charSet2.length), 1 );
            
            //return char1 + char2;
            return char1;
        }
        
        function randomID(size){
            var str = "";
            for(var i = 0; i < size; i++){
                str += getRandomChar();
            }
            return str;
        }
        
        return randomID(6) + utils.getCurrTimeStamp();
    },
    
    getCurrDate: function(){
        return new Date();
    },
    
    getCurrTimeStamp: function(){
        return Math.round(+new Date()/1000).toString();
    },
    
    //utils for array
    removeArrItem: function(array, ident){
        for(var i=0;i<array.length;i++){
            var arrayItem = array[i];
            if(arrayItem.ident == ident){
                //remove from the array
                array.splice(i ,1);
                arrayItem = null;
            }
        }
        return array;
    },
    
    getArrItem: function(array, ident){
        for(var i=0;i<array.length;i++){
            var arrayItem = array[i];
            if(arrayItem.ident == ident){
                return arrayItem;
            }
        }
    },
    
    checkRegexp: function( value, regexp) {
        if ( !( regexp.test( value ) ) ) {
            return false;
        } else {
            return true;
        }
    }
};

var currApp;